package server

import (
	"errors"
	"fmt"
	"github.com/gorilla/websocket"
)

var (
	NO_ENOUGH_CHIPS = errors.New("no enough chips")
)

type Player struct {
	Name  string
	Token string
	Hole  [2]Card
	Ready bool
	Bet   [4]int //每轮下注value
	Conn  *websocket.Conn
	Index int  //座位值
	Total int  //带入筹码数量
	Fold  bool //弃牌
}

func (p *Player) TotalChips() int {
	var total int
	for _, v := range p.Bet {
		if -1 != v {
			total += v
		}
	}
	return total
}

//设置下注值
func (p *Player) bet(betTurn int, chip int) error {
	if p.Total >= chip {
		if -1 == p.Bet[betTurn] {
			p.Bet[betTurn] = 0
		}
		p.Total = p.Total - (chip - p.Bet[betTurn])
		p.Bet[betTurn] = chip
		return nil
	}
	return errors.New("no more money")
}

func (p *Player) allIn(betTurn int) int {
	if -1 == p.Bet[betTurn] {
		p.Bet[betTurn] = p.Total
	} else {
		p.Bet[betTurn] += p.Total
	}
	p.Total = 0
	return p.Bet[betTurn]
}

func (p *Player) isAllin() bool {
	return p.Total <= 0
}

func (p *Player) isFold() bool {
	return p.Fold
}
func (p *Player) SendMsg(res Response) {
	p.Conn.WriteJSON(res)
}

func (p *Player) string() string {
	return fmt.Sprintf("%s,%d,%t,%v", p.Token, p.TotalChips, p.Ready, p.Hole)
}
